<template>
  <div class="app-container">

    <div style="text-align: center">
      <h3>距离归还还有一天的图书</h3>
    </div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="读者" prop="readerId">
        <el-select v-model="queryParams.readerId" filterable placeholder="请选择借阅人" style="width: 100%">
          <el-option
            v-for="item in readerList"
            :key="item.id"
            :label="item.readerName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">

      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left"  class="demo-table-expand">
            <el-row :gutter="20">
              <el-col :span="2">

              </el-col>
              <el-col :span="7">
                <el-form-item label="归还时间">
                  <span>{{ props.row.returnTime }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="7">
                <el-form-item label="超期天数">
                  <span>{{ props.row.overdueDays }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="超期金额">
                  <span>{{ props.row.overdueAmount }}</span>
                </el-form-item>
              </el-col>
            </el-row>



          </el-form>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="读者姓名" align="left" prop="readerName" />
      <el-table-column label="图书名称" align="left" prop="bookName"  width="200"/>
      <el-table-column label="借阅开始时间" align="center" prop="borrowStartTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.borrowStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="借阅结束时间" align="center" prop="borrowEndTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.borrowEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="借阅天数" align="center" prop="dayNum" />
      <el-table-column label="状态" align="center" prop="state" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.borrow_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <!--<el-table-column label="归还时间" align="center" prop="returnTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="应付金额" align="center" prop="money" />

      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="280">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleRenew(scope.row)"
            v-hasPermi="['borrow:info:xujie']"
            :disabled="scope.row.state==='2'||scope.row.state==='4'"
          >续借</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-finished"
            @click="handleReturn(scope.row)"
            v-hasPermi="['borrow:info:back']"
            :disabled="scope.row.state==='2'"
          >归还</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-finished"
            @click="remindReturn(scope.row)"
            v-hasPermi="['borrow:info:remind']"
            :disabled="scope.row.state==='2'"
          >提醒归还</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改借阅信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="借阅人姓名" prop="readerId">
          <el-select v-model="form.readerId" filterable placeholder="请选择借阅人" style="width: 100%">
            <el-option
              v-for="item in readerList"
              :key="item.id"
              :label="item.readerName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图书名称" prop="bookId">
          <el-select v-model="form.bookId" filterable placeholder="请选择图书" style="width: 100%" @change="handlerBook">
            <el-option
              v-for="item in bookList"
              :key="item.id"
              :label="item.bookName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="租金/日">
          <el-input v-model="form.rent" placeholder="请输入租金" disabled>
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="借阅开始时间" prop="borrowStartTime">
          <el-date-picker clearable
                          style="width: 100%"
                          v-model="form.borrowStartTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择借阅开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="借阅结束时间" prop="borrowEndTime">
          <el-date-picker clearable
                          style="width: 100%"
                          v-model="form.borrowEndTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          @change="handleDate"
                          placeholder="请选择借阅结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="借阅天数" prop="dayNum" v-show="showDay">
          <el-input v-model="form.dayNum"  disabled>
            <template slot="append">天</template>
          </el-input>
        </el-form-item>
        <el-form-item label="应付金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入应付金额" disabled>
            <template slot="append">元</template>
          </el-input>

        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择状态" clearable style="width: 100%" :disabled="this.form.id===null">
            <el-option
              v-for="dict in dict.type.borrow_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <!--<el-form-item label="归还时间" prop="returnTime">
          <el-date-picker clearable
                          style="width: 100%"
                          v-model="form.returnTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择归还时间">
          </el-date-picker>
        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="returntitle" :visible.sync="returnopen" width="600px" append-to-body>
      <el-form ref="returnForm" :model="returnForm"  label-width="120px" disabled>
        <el-form-item label="借阅人姓名" prop="readerId">
          <el-select v-model="returnForm.readerId" filterable placeholder="请选择借阅人" style="width: 100%">
            <el-option
              v-for="item in readerList"
              :key="item.id"
              :label="item.readerName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图书名称" prop="bookId">
          <el-select v-model="returnForm.bookId" filterable placeholder="请选择图书" style="width: 100%" @change="handlerBook">
            <el-option
              v-for="item in bookList"
              :key="item.id"
              :label="item.bookName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="超期租金/日">
          <el-input v-model="returnForm.rent*2"placeholder="请输入租金" disabled>
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="借阅开始时间" prop="borrowStartTime">
          <el-date-picker clearable
                          style="width: 100%"
                          v-model="returnForm.borrowStartTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择借阅开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="借阅结束时间" prop="borrowEndTime">
          <el-date-picker clearable
                          style="width: 100%"
                          v-model="returnForm.borrowEndTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          @change="handleDate"
                          placeholder="请选择借阅结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="超期天数" prop="overdueDays">
          <el-input v-model="returnForm.overdueDays" disabled>
            <template slot="append">天</template>
          </el-input>
        </el-form-item>
        <el-form-item label="超期金额" prop="overdueAmount">
          <el-input v-model="returnForm.overdueAmount" placeholder="请输入应付金额" disabled>
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitReturnForm">确 定</el-button>
        <el-button @click="returncancel">取 消</el-button>
      </div>
    </el-dialog>



  </div>

</template>


<script>
  import { listBorrow, getBorrow, delBorrow, addBorrow, updateBorrow,getOverDay ,getRemindReturn} from "@/api/book/borrow";
  import { listName } from "@/api/book/reader";
  import { listBookName,getInfo } from "@/api/book/info";

  export default {
    name: "Info",
    dicts: ['borrow_state'],
    data() {
      return {
        returntitle:'',
        returnopen:false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 借阅信息表格数据
        infoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          readerId: null,
          bookId: null,
          borrowStartTime: null,
          state: null,
          money: null,
          createUser: null,
          updateUser: null,
          returnTime: null,
          qurtyType: 'tixing'
        },
        // 表单参数
        form: {
          state:'1'
        },

        // 表单校验
        rules: {
          readerId: [
            { required: true, message: "借阅人不能为空", trigger: "change" }
          ],
          bookId: [
            { required: true, message: "图书名称不能为空", trigger: "change" }
          ],
          borrowStartTime: [
            { required: true, message: "借阅开始时间不能为空", trigger: "blur" }
          ],
          borrowEndTime: [
            { required: true, message: "借阅结束时间不能为空", trigger: "blur" }
          ],
          money: [
            { required: true, message: "应付金额不能为空", trigger: "change" }
          ]
        },
        readerList:[],
        readerParams: {
          readerName: null,
          email: null,
          state: null,
          idNumber: null,
          phoneNum: null,
          sex: null,
          createUser: null,
          updateUser: null,
        },
        bookList:[],
        bookParams: {
          bookName: null,
          bookType: null,
          bookInventory: null,
          author: null,
          press: null,
          unshelve: '0',
          createUser: null,
          updateUser: null
        },
        bookInfo:{},
        showRent:false,
        rent:0,
        showDay:false,
        renewSync:false,
        returnSync:false,
        returnForm:{}
      };
    },
    computed:{
      heaven:{
        get(){
          return (new Date(this.form.borrowEndTime)- new Date(this.form.borrowStartTime))/3600/1000/24+1
        }
      }
    },
    created() {
      this.getList();
      this.getReaderList()
      this.getBookList()
    },
    methods: {
      goTarget(href) {
        window.open(href, "_blank");
      },
      /** 查询借阅信息列表 */
      getList() {
        this.loading = true;
        listBorrow(this.queryParams).then(response => {
          this.infoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      returncancel() {
        this.returnopen= false;
        this.resetReturn()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          readerId: null,
          bookId: null,
          borrowStartTime: null,
          borrowEndTime: null,
          state:'1',
          money: null,
          delFlag: null,
          remark: null,
          createTime: null,
          createUser: null,
          updateUser: null,
          updateTime: null,
          returnTime: null,
          rent:null
        };
        this.resetForm("form");
      },
      resetReturn() {
        this.returnForm = {
          id: null,
          readerId: null,
          bookId: null,
          borrowStartTime: null,
          borrowEndTime: null,
          state:null,
          money: null,
          delFlag: null,
          remark: null,
          createTime: null,
          createUser: null,
          updateUser: null,
          updateTime: null,
          returnTime: null,
          rent:null
        };
        this.resetForm("returnForm");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.getReaderList()
        this.getBookList()
        this.showRent = false
        this.open = true;
        this.showDay = false
        this.title = "添加借阅信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.getReaderList()
        this.getBookList()
        const id = row.id || this.ids
        getBorrow(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.showRent = true
          this.showDay = true
          this.title = "修改借阅信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateBorrow(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addBorrow(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除借阅信息编号为"' + ids + '"的数据项？').then(function() {
          return delBorrow(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('book/info/export', {
          ...this.queryParams
        }, `info_${new Date().getTime()}.xlsx`)
      },
      getReaderList() {
        listName(this.readerParams).then(response => {
          this.readerList = response.list
        });
      },
      getBookList() {
        listBookName(this.bookParams).then(response => {
          this.bookList = response.list
        });
      },
      handlerBook(data){
        getInfo(data).then(response => {
          this.bookInfo = response.data;
          this.form.rent = this.bookInfo.rent
          this.showRent = true
        });
      },
      handleDate(date){
        this.form.dayNum = this.heaven
        this.showDay = true
        this.form.money = this.form.dayNum * this.form.rent
      },

      handleRenew(rowData){
        this.form = rowData
        this.form.state = '2'
        updateBorrow(this.form).then(response => {
          this.reset();
          this.getReaderList()
          this.getBookList()
          this.open = true;
        });
        /*this.showRent = true
        this.showDay = true*/

      },
      handleReturn(rowData){
        this.form = rowData
        if(this.form.state!=='4'){
          this.form.state = '2'
          updateBorrow(this.form).then(response => {
            this.$modal.msgSuccess("归还成功");
            this.open = false;
            this.getList();
          });
        }else{
          getOverDay(this.form.id).then(response => {
            this.resetReturn()
            this.returnForm = response.data;
            this.getReaderList()
            this.getBookList()
            this.returnopen = true;
            this.returntitle = "超期信息";
          });
        }

      },
      submitReturnForm() {
        this.$refs["returnForm"].validate(valid => {
          if (valid) {
            this.returnForm.state = '2'
            updateBorrow(this.returnForm).then(response => {
              this.$modal.msgSuccess("归还成功");
              this.returnopen = false;
              this.getList();
            });
          }
        });
      },
      remindReturn(rowData){
        getRemindReturn(rowData.id).then(response => {
          this.$modal.msgSuccess(response.msg);
        });
      }
    }
  };
</script>



<style scoped lang="scss">
.home {
  blockquote {
    padding: 10px 20px;
    margin: 0 0 20px;
    font-size: 17.5px;
    border-left: 5px solid #eee;
  }
  hr {
    margin-top: 20px;
    margin-bottom: 20px;
    border: 0;
    border-top: 1px solid #eee;
  }
  .col-item {
    margin-bottom: 20px;
  }

  ul {
    padding: 0;
    margin: 0;
  }

  font-family: "open sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 13px;
  color: #676a6c;
  overflow-x: hidden;

  ul {
    list-style-type: none;
  }

  h4 {
    margin-top: 0px;
  }

  h2 {
    margin-top: 10px;
    font-size: 26px;
    font-weight: 100;
  }

  p {
    margin-top: 10px;

    b {
      font-weight: 700;
    }
  }

  .update-log {
    ol {
      display: block;
      list-style-type: decimal;
      margin-block-start: 1em;
      margin-block-end: 1em;
      margin-inline-start: 0;
      margin-inline-end: 0;
      padding-inline-start: 40px;
    }
  }
}
</style>

